/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: GodK
 * Date: 2024-07-15
 * Time: 20:28
 */
import java.util.*;
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int a = in.nextInt();
        int b = -1;
        int c = -1;
        int ret = Integer.MAX_VALUE;
        String n = in.nextLine();
        String str1 = in.nextLine();
        String str2 = in.nextLine();
        String[] str = new String[a];
        for(int i = 0;i < a;i++){
            str[i] = in.nextLine();
        }
        if(str1 == null||str2 == null){
            System.out.print(-1);
            return;
        }
        for(int i = 0;i < a;i++){
            if(str1.equals(str[i]))b = i;
            else if(str2.equals(str[i]))c = i;
            if(b != -1 && c != -1){
                ret = Math.min(Math.abs(b-c),ret);
            }
        }
        if(ret == Integer.MAX_VALUE){
            System.out.print(-1);
            return;
        }else{
            System.out.print(ret);
            return;
        }
    }
}
